package com.ruoyi.fun.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fun.domain.SysDeploymentLog;
import com.ruoyi.fun.service.ISysDeploymentLogService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 部署记录Controller
 * 
 * @author Dreamyy
 * @date 2025-09-10
 */
@RestController
@RequestMapping("/fun/deploymentLog")
public class SysDeploymentLogController extends BaseController
{
    @Autowired
    private ISysDeploymentLogService sysDeploymentLogService;

    /**
     * 查询部署记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysDeploymentLog sysDeploymentLog)
    {
        startPage();
        List<SysDeploymentLog> list = sysDeploymentLogService.selectSysDeploymentLogList(sysDeploymentLog);
        return getDataTable(list);
    }

    /**
     * 导出部署记录列表
     */
    @Log(title = "部署记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDeploymentLog sysDeploymentLog)
    {
        List<SysDeploymentLog> list = sysDeploymentLogService.selectSysDeploymentLogList(sysDeploymentLog);
        ExcelUtil<SysDeploymentLog> util = new ExcelUtil<SysDeploymentLog>(SysDeploymentLog.class);
        util.exportExcel(response, list, "部署记录数据");
    }

    /**
     * 获取部署记录详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysDeploymentLogService.selectSysDeploymentLogById(id));
    }

    /**
     * 新增部署记录
     */
    @Log(title = "部署记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysDeploymentLog sysDeploymentLog)
    {
        return toAjax(sysDeploymentLogService.insertSysDeploymentLog(sysDeploymentLog));
    }

    /**
     * 修改部署记录
     */
    @Log(title = "部署记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysDeploymentLog sysDeploymentLog)
    {
        return toAjax(sysDeploymentLogService.updateSysDeploymentLog(sysDeploymentLog));
    }

    /**
     * 删除部署记录
     */
    @Log(title = "部署记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysDeploymentLogService.deleteSysDeploymentLogByIds(ids));
    }

    /**
     * 部署完成回调接口（无权限验证）
     */
    @PostMapping("/callback/{id}")
    public AjaxResult deploymentCallback(@PathVariable Long id)
    {
        int result = sysDeploymentLogService.completeDeployment(id);
        if (result > 0) {
            return AjaxResult.success("部署完成回调成功");
        } else {
            return AjaxResult.error("部署记录不存在或更新失败");
        }
    }

    /**
     * 部署失败回调接口（无权限验证）
     */
    @PostMapping("/callback/failure/{id}")
    public AjaxResult deploymentFailureCallback(@PathVariable Long id, @RequestBody String errorMessage)
    {
        int result = sysDeploymentLogService.failDeployment(id, errorMessage);
        if (result > 0) {
            return AjaxResult.success("部署失败回调成功");
        } else {
            return AjaxResult.error("部署记录不存在或更新失败");
        }
    }
}
